//
//  CellForPhotoCollectList.m
//  世界
//
//  Created by dllo on 16/3/1.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "CellForPhotoCollectList.h"
#import "CellForPhotoList.h"
#import <MJRefresh.h>
#import <UIImageView+WebCache.h>
#import "DataBaseForPhoto.h"
#import "ModelForForPhotoDetail.h"

@interface CellForPhotoCollectList ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tabView;
@property (nonatomic, retain) NSMutableArray *arrForCollect;

@end

@implementation CellForPhotoCollectList



#pragma mark 重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self != nil)
    {
        [[DataBaseForPhoto shareDataBaseHandle] open];
        
        self.arrForCollect = [[DataBaseForPhoto shareDataBaseHandle] select];
        
        //创建子控件
        [self createTabView];
        
        [[NSNotificationCenter defaultCenter] addObserverForName:@"cccc" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
            
            NSString *str = [note.userInfo objectForKey:@"sele"];
            
            BOOL bb ;
            
            if ([str isEqualToString:@"编辑"])
            {
                bb = YES;
            }
            else
                bb = NO;
            
            [self.tabView setEditing:bb animated:YES];
        }];
        
    }
    return self;
}




#pragma mark 提交点击
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    ModelForForPhotoDetail *model3 = self.arrForCollect[indexPath.row];
    
    [[DataBaseForPhoto shareDataBaseHandle]deleteWithCode:model3.title];
    
     //从数据源删除数据
    self.arrForCollect = [[DataBaseForPhoto shareDataBaseHandle] select];
    
    //tableView删除cell
    [self.tabView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}







#pragma mark 子控件的布局
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    //collectionCell 的子控件的布局通常在此方法中设置
    [super applyLayoutAttributes:layoutAttributes];
    
    //设置tabView的空间位置及大小
    self.tabView.frame = self.frame;
}




#pragma mark 自定义方法 >>>>>>
#pragma mark 创建TabView
- (void)createTabView
{
    self.tabView = [[UITableView alloc] init];
    
    [self.contentView addSubview:self.tabView];
    
    [_tabView release];
    
    //指定代理人
    self.tabView.dataSource = self;
    self.tabView.delegate = self;
    
    //注册Cell
    [self.tabView registerClass:[CellForPhotoList class] forCellReuseIdentifier:@"pool"];
    //去掉分割线
    self.tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
}




#pragma mark UITableViewDataSource 协议方法
#pragma mark 相应分区的行的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrForCollect.count;
}




#pragma mark 给每行添加cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    CellForPhotoList *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    //去掉选择cell之后背景颜色灰的效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    ModelForForPhotoDetail *model = [self.arrForCollect objectAtIndex:indexPath.row];
    
    cell.lableForTitle.text = model.title;
    
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.url] placeholderImage:[UIImage imageNamed:@"占位图"]];
    
    return cell;
}




#pragma mark UITableViewDelegate 协议方法
#pragma mark 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}




#pragma mark 选择相应的行所触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        NSString *str = [NSString stringWithFormat:@"%ld", indexPath.row];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PhotoCollectDetail" object:nil userInfo:@{@"number" : str}];

}




@end
